package com.qingzhou.web.service.impl.convert;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.fastjson2.JSON;
import com.qingzhou.web.service.impl.params.vo.AdjustDetailVo;
import com.qingzhou.web.service.util.DictUtil;

import java.util.List;
import java.util.stream.Collectors;

public class PreAdjustDetailConverter implements Converter<String> {
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        WriteCellData<Object> emptyData = new WriteCellData<>("");
        emptyData.setType(CellDataTypeEnum.STRING);
        if(StrUtil.isEmpty(value)) {
            return emptyData;
        }

        return new WriteCellData<>(PreAdjustDetailConverter.getConvertString(value));
    }

    public static String getConvertString(String value){
        if(StrUtil.isEmpty(value)) return "";

        List<AdjustDetailVo> lifeEvaluationVos = JSON.parseArray(value, AdjustDetailVo.class);
        String tpl = "调整方式：{}，调整日期：{}，调整原因：{}，调整后方案：{}";
        List<String> collect = lifeEvaluationVos.stream().map(item -> {
            String adjustMethod = item.getAdjustMethod();
            if(StrUtil.isNotEmpty(adjustMethod)){
                adjustMethod = DictUtil.getLabel(adjustMethod,"adjust_method_dict");
            }

            String adjustReason = item.getAdjustReason();
            if(StrUtil.isNotEmpty(adjustReason)){
                if(StrUtil.equals("other",adjustReason)){
                    adjustReason = item.getAdjustReasonOtherDetail();
                }else{
                    adjustReason = DictUtil.getLabel(adjustReason,"pre_tz_reason");
                }
            }
            return StrUtil.format(tpl, adjustMethod, item.getAdjustDate(), adjustReason, item.getAdjustPlan());
        }).collect(Collectors.toList());
        return CollUtil.join(collect,"\n");
    }
}
